﻿using Sunny.UI.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
using System.Text;

namespace CoreShop.PrintService
{
    public class JbPrinter
    {
        public LoadPOSDll PosPrint = null;
        public string printerIP { get; set; }
        public IntPtr Gp_IntPtr;//驱动打印句柄
        public uint nWidthTimes = 0;
        public uint nHeightTimes = 0;
        //public int textAlignMode = 0;//对齐方式  Left = 0,  Middle = 1, Right = 2
        public int textFontSize = 1;
        public int nRowHeight = 0;//行高

        private static string Const_Buzzing = "1B 42 03 01";

        /// <summary>
        /// 设置行间距为0
        /// </summary>
        /// private static Byte[] Const_Row0 = new Byte[] { 0x1b, 0x33, 0x00 };
        private static Byte[] Const_Row0 = new Byte[] { 0x1b, 0x33, 0x10 };
        private static Byte[] Const_Row1 = new Byte[] { 0x1b, 0x33, 0x25 };

        /// <summary>
        /// 关闭打印机
        /// </summary>
        /// <param name="printername"></param>
        /// <returns></returns>
        public Boolean CloseNetPor()
        {
            LoadPOSDll.POS_Close();
            return true;
        }

        /// <summary>
        /// 切纸
        /// </summary>
        /// <returns></returns>
        public int Cut()
        {
            LoadPOSDll.POS_CutPaper(LoadPOSDll.POS_CUT_MODE_PARTIAL, 0);
            return 1;
        }

        /// <summary>
        /// 打印空行
        /// </summary>
        /// <returns></returns>
        public bool PrintLine()
        {
            LoadPOSDll.POS_S_TextOut(" ", 0, nWidthTimes, nHeightTimes, 0, 0);
            LoadPOSDll.POS_FeedLine();
            return false;
        }

        /// <summary>
        /// 设置字体大小;
        /// </summary>
        /// <param name="bold"></param>
        /// <returns></returns>
        public bool SetFontSize(int sSize)
        {
            textFontSize = sSize;
            switch (sSize)
            {
                case 2:
                    nWidthTimes = 1;
                    nHeightTimes = 2;
                    break;
                case 3:
                    nWidthTimes = 2;
                    nHeightTimes = 3;
                    break;
                default:
                    nWidthTimes = 1;
                    nHeightTimes = 1;
                    break;
            }
            LoadPOSDll.POS_S_TextOut("", 0, nWidthTimes, nHeightTimes, 0, 0); 
            return true;
        }

        public string CenterString(string original, int width)
        {
            int padding = width - original.Length;
            if (padding % 2 == 0) // 如果需要填充的空格是偶数
            {
                return original.PadLeft(padding / 2 + original.Length).PadRight(width);
            }
            else // 如果需要填充的空格是奇数
            {
                return original.PadLeft(padding / 2 + original.Length + 1).PadRight(width);
            }
        }

        /// <summary>
        /// 打印文本.在调用本函数之前必须先调用正确的 设置字体、左边距
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public bool PrintText(string content)
        {
            byte[] by_SendData;
            if (content.Length < 1) content = "  ";
            //Gp_IntPtr = LoadPOSDll.POS_S_TextOut(" ", 0, nWidthTimes, nHeightTimes, 0, 0);
            //Gp_IntPtr = LoadPOSDll.POS_FeedLine();
            //if ((int)Gp_IntPtr > 0) return true;
            by_SendData = Processor.StringToBytes(content);
            if (LoadPOSDll.POS_StartDoc())
            {
                LoadPOSDll.POS_WriteFile(LoadPOSDll.POS_IntPtr, by_SendData, (uint)by_SendData.Length);
                LoadPOSDll.POS_WriteFile(LoadPOSDll.POS_IntPtr, new byte[] { 0x0a }, 1);
                LoadPOSDll.POS_EndDoc();
                return true;
            }
            return false;
        }

        /// <summary>
        /// 设置行距xyPrinter.
        /// </summary>
        /// <param name="bold"></param>
        /// <returns></returns>
        public bool SetRowHeight(int rowType)
        {
            //if (rowType == 0) LoadPOSDll.POS_SetLineSpacing(33);
            //else if (rowType == 1) LoadPOSDll.POS_SetLineSpacing(33);
            //return true;
            //return PrintText(Const_Row0);
            nRowHeight = rowType;
            if (rowType == 0) return PrintText(Const_Row0);
            bool bl = PrintText(Const_Row1);
            return bl;
        }

        //设置对齐方式
        public const string Const_SetAlign = "1B 61 ";
        public const string Const_Align_Left = "30";
        public const string Const_Align_Middle = "31";
        public const string Const_Align_Right = "32";
        /// <summary>
        /// 设置对齐方式
        /// </summary>
        /// <param name="left"></param>
        /// <returns></returns>
        public bool SetAlignMode(eTextAlignMode alignmode)
        {
            //textAlignMode = (int)alignmode;

            string code = string.Empty;
            switch (alignmode)
            {
                case eTextAlignMode.Left:
                    code = Const_Align_Left;
                    break;
                case eTextAlignMode.Middle:
                    code = Const_Align_Middle;
                    break;
                case eTextAlignMode.Right:
                    code = Const_Align_Right;
                    break;
                default:
                    code = Const_Align_Left;
                    break;
            }

            //注意：先低字节后高字节 
            string str = Const_SetAlign + code;
            byte[] mybyte = null;
            Processor.HexStringToBytes(str, out mybyte);
            bool bl = PrintText(mybyte);
            return true;
        }

        public bool PrintText(byte[] content)
        {
            if (LoadPOSDll.POS_StartDoc())
            {
                LoadPOSDll.POS_WriteFile(LoadPOSDll.POS_IntPtr, content, (uint)content.Length);
                LoadPOSDll.POS_WriteFile(LoadPOSDll.POS_IntPtr, new byte[] { 0x0a }, 1);                
                return LoadPOSDll.POS_EndDoc();
            }
            return false;
        }

        /// <summary>
        /// 打印机来单打印蜂鸣提示2024-05-04Jonson
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        public bool SetBuzzing()
        {
            byte[] mybyte = null;            
            Processor.HexStringToBytes(Const_Buzzing, out mybyte);
            bool bl = PrintText(mybyte);
            return bl;
        }

        /// <summary>
        /// 查询打印机状态
        /// </summary>
        /// <param name="printername"></param>
        /// <returns></returns>
        public int GetPrinterStatus(string ipAddress)
        {
            Byte pszStatus;
            return LoadPOSDll.POS_NETQueryStatus(ipAddress, out pszStatus);
        }

        /// <summary>
        /// 设备打开后的句柄
        /// </summary>
        public IntPtr m_Inited;

        /// <summary>
        /// 连接打印机
        /// </summary>
        /// <param name="printername"></param>
        /// <returns></returns>
        public IntPtr ConnectNetPort(string ipAddr)
        {
            m_Inited = LoadPOSDll.POS_Open(ipAddr, 0, 0, 0, 0, LoadPOSDll.POS_OPEN_NETPORT);
            return m_Inited;
        }

        /// <summary>
        /// 打印测试页
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public bool PrintTestPage()
        {
            if (LoadPOSDll.POS_StartDoc())
            {
                //打印内容
                byte[] by_SendData = System.Text.Encoding.Default.GetBytes("Y\r\n");
                LoadPOSDll.POS_WriteFile(LoadPOSDll.POS_IntPtr, by_SendData, (uint)by_SendData.Length);
                LoadPOSDll.POS_WriteFile(LoadPOSDll.POS_IntPtr, new byte[] { 0x0a }, 1);                
                return LoadPOSDll.POS_EndDoc();
            }
            return false;
        }

    }
}
